/*
 * @Author: ziqi jhzq12345678
 * @Date: 2024-08-19 14:04:12
 * @LastEditors: ziqi jhzq12345678
 * @LastEditTime: 2025-01-07 16:24:31
 * @FilePath: /pornhub_app/lib/src/modules/search/search_page.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import 'package:pornhub_app/utils/extension.dart';
import 'package:pornhub_app/views/search/search_controller.dart';
import 'package:pornhub_app/views/search/search_history.dart';
import 'package:pornhub_app/views/search/search_hotword.dart';

import 'package:pornhub_app/routes/routes.dart';

import 'package:pornhub_app/utils/color.dart';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class SearchPage extends StatefulWidget {
  const SearchPage({super.key});

  @override
  State createState() => _SearchPage();
}

class _SearchPage extends State<SearchPage> {
  TextEditingController searchTextController = TextEditingController();
  final searchvc = Get.find<SearchPageController>();
  void handleSearch([String? v]) {
    if (searchTextController.text.isEmpty && v == null) return;
    String word = v ?? searchTextController.text;
    searchvc.addHistory(word);
    Get.toNamed(Routes.searchresult, arguments: {'word': word});
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          shadowColor: const Color.fromRGBO(0, 0, 0, 0),
          surfaceTintColor: COLOR.hexColor('#ffffff'),
          automaticallyImplyLeading: false,
          titleSpacing: 0,
          title: Flex(
            direction: Axis.horizontal,
            children: [
              IconButton(
                padding: EdgeInsets.only(left: 5.w),
                iconSize: 24.w,
                icon: Icon(
                  Icons.arrow_back_ios,
                  color: COLOR.hexColor('#ffffff'),
                ),
                onPressed: () => Navigator.pop(context),
              ),
              Expanded(
                  child: Container(
                      width: 296.w,
                      height: 30.w,
                      clipBehavior: Clip.hardEdge,
                      alignment: Alignment.center,
                      margin: EdgeInsets.only(right: 20.w),
                      padding: EdgeInsets.symmetric(horizontal: 15.w),
                      decoration: BoxDecoration(
                        border: Border.all(color: Colors.white),
                        borderRadius: BorderRadius.circular(15.w),
                      ),
                      child: Flex(
                        direction: Axis.horizontal,
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Image.asset(
                            'assets/search/ic_search.png',
                            width: 16.w,
                            height: 16.w,
                          ),
                          Padding(padding: EdgeInsets.only(left: 8.w)),
                          Expanded(
                              child: TextField(
                            style:
                                TextStyle(color: Colors.white, fontSize: 14.w),
                            controller: searchTextController,
                            onSubmitted: (value) {
                              searchvc.addHistory(value);
                              handleSearch(value);
                            },
                            decoration: InputDecoration(
                                isCollapsed: true,
                                hintText: "请输入搜索内容",
                                hintStyle: TextStyle(
                                    color: COLOR.hexColor('#898A8E'),
                                    fontSize: 14.w),
                                enabledBorder: const UnderlineInputBorder(
                                    borderSide: BorderSide(
                                        color: Color.fromRGBO(0, 0, 0, 0))),
                                focusedBorder: const UnderlineInputBorder(
                                    borderSide: BorderSide(
                                        color: Color.fromRGBO(0, 0, 0, 0)))),
                          ))
                        ],
                      ))),
              GestureDetector(
                onTap: handleSearch,
                child: Text(
                  '搜索',
                  style: TextStyle(
                      color: COLOR.hexColor('#B93FFF'), fontSize: 15.w),
                ),
              ).paddingRight(14.w)
            ],
          ),
        ),
        body: Padding(
            padding: EdgeInsets.symmetric(horizontal: 14.w, vertical: 0.w),
            child: const Column(
              children: [
                SearchHistory(),
                SearchHotWord(),
              ],
            )));
  }
}
